package accepttests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import database.CleanPersistence;
import database.DataseConsult;
import database.UIDManger;
import database.rescue.GetUserDatabase;
import sessionManager.SessionManager;
import user.User;
import user.UserIF;
import user.blog.Blog;
import user.blog.BlogIF;
import user.blog.FlagsPost;
import user.blog.post.Post;
import user.blog.post.comments.Comment;
import user.blog.post.comments.CommentFlags;

public class SubBlogsAndCommentsFacade {

	SessionManager sessionManagerInstance = SessionManager.getInstance();
	private List<Blog> subBlogs = new ArrayList<Blog>();
	private List<Post> posts = new ArrayList<Post>();
	private List<Comment> comments = new ArrayList<Comment>();
	private List<Comment> subComments = new ArrayList<Comment>();

	public void cleanPersistence() {
		new CleanPersistence().cleanPersistence();
	}

	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		ProfileFacade pf = new ProfileFacade();
		pf.createProfile(login, senha, nome_exibicao, email, sexo, dataNasc,
				endereco, interesses, quem_sou_eu, filmes, musicas, livros);
	}

	public String logon(String login, String password) throws Exception {
		return sessionManagerInstance.loginUser(login, password);

	}

	public String createBlog(String sessionId, String titulo, String descricao)
			throws Exception {
		UserIF userTemp = sessionManagerInstance.getUserBySessionID(sessionId);
		String blogID = userTemp.createBlog(sessionId, titulo, descricao);
		userTemp.save();
		return blogID;
	}

	public String createSubBlog(String sessionId, String blogPai, String title,
			String descricao) throws Exception {
		UserIF userTemp = sessionManagerInstance.getUserBySessionID(sessionId);
		String subBlogID = UIDManger.getValidBlgoID();
		Blog subBlog = new Blog(subBlogID, sessionId, title, descricao);
		subBlog.getProperties().setProperty(FlagsPost.OWNER.getFlag(), blogPai);
		subBlogs.add(subBlog);
		userTemp.save();
		return subBlogID;
	}

	public Integer getNumberOfBlogsByLogin(String login) throws Exception {

		return sessionManagerInstance.getNumberOfBlogsByLogin(login);
	}

	public Integer getNumberOfBlogsBySessionId(String login) throws Exception {
		String loginTemp = sessionManagerInstance.getUserBySessionID(login)
				.getLogin();
		return sessionManagerInstance.getNumberOfBlogsByLogin(loginTemp);
	}

	public Integer getNumberOfSubBlogs(String blogId) throws Exception {

		int count = 0;
		Iterator<Blog> it = subBlogs.iterator();
		while (it.hasNext()) {
			Blog blog = (Blog) it.next();
			Properties p = blog.getProperties();
			if (p.getProperty(FlagsPost.OWNER.getFlag()).equals(blogId)) {
				count++;
			}
		}
		return count;
	}

	public Integer getNumberOfAllSubBlogs(String blogId) throws Exception {
		int count = 0;
		Iterator<Blog> it = subBlogs.iterator();
		while (it.hasNext()) {
			Blog blog = (Blog) it.next();
			Properties p = blog.getProperties();
			if (p.getProperty(FlagsPost.OWNER.getFlag()).equals(blogId)) {
				count += getNumberOfAllSubBlogs(blog.getBlogID());
				count++;
			}
		}
		return count;
	}

	public String getSubBlog(String blogId, int index) throws Exception {
		int count = 0;
		String blogIDProcurada = "";
		Iterator<Blog> it = subBlogs.iterator();

		while (it.hasNext() || count <= index) {
			Blog blog = (Blog) it.next();
			Properties p = blog.getProperties();
			if (p.getProperty(FlagsPost.OWNER.getFlag()).equals(blogId)) {
				blogIDProcurada = blog.getBlogID();
				count++;
			} else {
				count++;
			}

		}

		return blogIDProcurada;
	}

	public String createPost(String sessionId, String blogId, String titulo,
			String texto) throws Exception {

		User user = (User) sessionManagerInstance.getUserBySessionID(sessionId);

		Post post = new Post(sessionId, blogId, titulo, texto);
		String postID = "";
		try {
			postID = user.getBlogByBID(blogId).addPost(post);
		} catch (Exception e) {
			Iterator<Blog> it = subBlogs.iterator();

			while (it.hasNext()) {
				Blog blogSearched = (Blog) it.next();
				if (blogSearched.getBlogID().equals(blogId)) {
					postID = blogSearched.getBlogID();
				}
			}
		}

		posts.add(post);
		user.save();

		return postID;
	}

	public Integer getNumberOfPosts(String blogId) throws Exception {
		BlogIF blog = sessionManagerInstance.getBlogByBID(blogId);

		return blog.getNumberOfPosts();
	}

	public Integer getNumberOfAllPosts(String blogId) throws Exception {
		Integer count = 0;
		Iterator<Post> it = posts.iterator();
		List<String> subBlogsIDOf = getSubBlogsIDOf(blogId);
		while (it.hasNext()) {
			Post post = (Post) it.next();
			if (post.getBlogId().equals(blogId)) {
				count++;
			}
			if (subBlogsIDOf != null) {
				if (subBlogsIDOf.contains(post.getBlogId())) {
					count++;
				}
			}
		}

		return count;
	}

	private List<String> getSubBlogsIDOf(String fatherID) {
		List<String> subBlogsSearched = new ArrayList<String>();
		Iterator<Blog> it = subBlogs.iterator();
		while (it.hasNext()) {
			Blog subBlog = (Blog) it.next();
			Properties p = subBlog.getProperties();
			String blogFatherID = p.getProperty(FlagsPost.OWNER.getFlag());
			if (blogFatherID != null | blogFatherID.equals(fatherID)) {
				subBlogsSearched.add(subBlog.getBlogID());
			}
		}
		return subBlogsSearched;
	}

	public String addComment(String sessionId, String postId, String text)
			throws Exception {
		Post post = getPost(postId);

		if (post == null) {
			return "";
		}
		Comment c = new Comment(text, postId, sessionId);
		comments.add(c);
		String commentId = post.addComment(sessionId, text);
		sessionManagerInstance.getUserBySessionID(sessionId).save();
		return commentId;
	}

	private Post getPost(String postId) {
		Iterator<Post> it = posts.iterator();
		while (it.hasNext()) {
			Post post = (Post) it.next();
			if (post.getPostID().equals(postId)) {
				return post;
			}

		}
		return null;
	}

	public String addSubComment(String sessionId, String idComment, String text)
			throws Exception {
		Comment c = new Comment(text, idComment, sessionId);
		subComments.add(c);
		sessionManagerInstance.getUserBySessionID(sessionId).save();
		return c.getProperty(CommentFlags.CID);
	}

	public Integer getNumberOfSubComments(String idComment) {
		Integer count = 0;
		Iterator<Comment> it = subComments.iterator();
		while (it.hasNext()) {
			Comment subComment = (Comment) it.next();
			if (subComment.getProperty(CommentFlags.AUTHOR).equals(idComment)) {
				count++;
			}

		}
		return count;

	}

	public Integer getNumberOfAllSubComments(String idComment) {
		Integer count = getNumberOfSubComments(idComment);
		String subSubComment = getSubComment(idComment, 0);
		if (!subSubComment.equals("")) {
			count += getNumberOfSubComments(subSubComment);
		}
		return count;
	}

	public String getSubComment(String idComment, Integer index) {
		int count = 0;
		String cId = "";
		Iterator<Comment> it = subComments.iterator();

		while (it.hasNext() || count <= index) {
			Comment c = (Comment) it.next();
			String comment = c.getProperty(CommentFlags.AUTHOR);
			if (comment.equals(idComment)) {
				cId = c.getProperty(CommentFlags.CID);
				count++;
			} else {
				count++;
			}

		}
		return cId;
	}
}